package com.share.example.stock.utils;

import com.alibaba.fastjson.JSON;
import com.share.common.utils.Base64Util;
import com.share.common.utils.FileUtil;
import com.share.common.utils.HttpUtil;
import com.share.common.utils.NumUtil;

import java.io.File;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

public class OcrCode {
    public static void main(String[] args) {
        Map<String, Set> reMap = new HashMap<>();
        String baseDir = "C:\\Users\\Administrator\\Desktop\\code";

        File[] files = new File(baseDir).listFiles();
        AtomicInteger count = new AtomicInteger(0);
        for (File file : files) {
            if (file.isFile()) {
                continue;
            }

            String key = file.getName();
            TreeSet reSet = ansOneDir(file, count);
            reMap.put(key, reSet);

            //中途也打印下,比较方便处理
            String join = String.join(",", reSet);
            System.out.println("类别=" + key + ",赛选条数=" + reSet.size() + ",结果： " + join);
        }

        //遍历结果
        for (String key : reMap.keySet()) {
            Set reSet = reMap.get(key);
            String join = String.join(",", reSet);
            System.out.println("最后类别=" + key + ",赛选条数=" + reSet.size() + ",结果： " + join);
        }
    }

    //分析目录中的多个文件
    public static TreeSet ansOneDir(File pFile, AtomicInteger count) {
        TreeSet reSet = new TreeSet();
        String url = "http://192.168.88.211:9032/base64";
        List<File> files = FileUtil.listFile(pFile);
        for (File file : files) {
            if (file.isDirectory()) {
                continue;
            }
            count.incrementAndGet();
            System.out.println("正在处理第n张图num=" + count.get());
            List<String> list = reqOcr(url, file);
            reSet.addAll(list);
        }

        return reSet;
    }


    //发起请求
    public static List<String> reqOcr(String url, File imageFile) {
        //读取文件
        byte[] bytes = FileUtil.readByte(imageFile);
        String encode = Base64Util.encode(bytes);

        //发起http请求
        Map<String, Object> map = new HashMap<>();
        map.put("image", encode);
        try {
            String post = HttpUtil.post(url, JSON.toJSONString(map), 10000);
            return getCode(post);
        } catch (Exception e) {
            e.printStackTrace();
            return new LinkedList<>();
        }
    }

    //数据分析
    public static List<String> getCode(String strParam) {
        List<String> reList = new LinkedList<>();
        if (strParam == null || strParam.length() < 1) {
            return reList;
        }
        String[] split = strParam.split("\\[\\\"");
        int length = split.length;
        for (int i = 0; i < length; i++) {
            String[] split2 = split[i].split(", ");
            int len2 = split2.length;
            for (int j = 0; j < len2; j++) {
                String s = split2[j];
                int slen = s.length();

                if (s.contains(".") || s.contains("[") || slen < 6) {
                    continue;
                }

                String str6 = s.substring(0, 6);
                if (!NumUtil.isNum(str6)) {
                    continue;
                }

                if (str6.startsWith("9")) {
                    continue;
                }

                if (slen > 6) {
                    reList.add(str6);
                }
            }
        }

        return reList;
    }
}
